#!/bin/sh
# PCP QA Test No. 1131
# Exercise pcp2json, pcp2openmetrics, and pcp2opentelemetry.
#
# Copyright (c) 2025 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pcp2json >/dev/null 2>&1 || _notrun "pcp2json not installed"
which pcp2openmetrics >/dev/null 2>&1 || _notrun "pcp2openmetrics not installed"
which pcp2opentelemetry >/dev/null 2>&1 || _notrun "pcp2opentelemetry not installed"

_cleanup()
{
    cd $here
    _restore_config $PCP_SYSCONF_DIR/labels
    $sudo rm -rf $tmp.*
}

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
trap "_cleanup; exit \$status" 0 1 2 3 15

A="$here/archives/rep"
hostname=`hostname`
machineid=`_machine_id`
domainid=`_domain_name`

_archive_filter()
{
    sed -e "s,$A,ARCHIVE_PATH,g"
}

_value_filter()
{
    sed 's/\(\.[0-9]\)[0-9]*/\1/g'
}

_filter_pcp2openmetrics()
{
    # Note: need PMID filter for each platform, where the PMID of
    #       hinv.ncpu may be different
    #
    tee -a $seq_full \
    | col -b \
    | sed \
	-e '/hostname=/s/'$hostname'/HOST/' \
	-e '/domainname=/s/'$domainid'/DOMAINID/' \
	-e '/machineid=/s/'$machineid'/MACHINEID/' \
	-e '/groupid=/s/groupid="*[0-9][0-9]*"*/groupid="GROUPID"/' \
	-e '/userid=/s/userid="*[0-9][0-9]*"*/userid="USERID"/' \
	-e '/version=/s/version="*[^"]*"*/version="VERSION"/' \
	-e '/agent=/s/agent="*[^"]*"*/agent="AGENT"/' \
	-e 's/\(.*}\).*/\1 NCPU/' \
	-e '/PCP5 hinv_ncpu/{
s/ 60.0.32 / PMID /
s/ 85.0.0 / PMID /
}' \
    | LC_COLLATE=POSIX sort
}

_filter_pcp2opentelemetry()
{
   tee -a $here/$seq.full \
   | col -b \
   | sed \
       -e '/\"asInt\":/ s/[0-9][0-9]*/INTEGER/' \
       -e '/\"asDouble\":/ s/[0-9][0-9]*/DOUBLE/' \
       -e 's/\"version\": .*/\"version\": VERSION/' \
       -e 's/\"stringValue\": \".*\"/\"stringValue\": \"STRING\"/' \
       -e 's/\"timeUnixNano\": [0-9][0-9]*\.[0-9][0-9]*.*/\"timeUnixNano\": TIME/' \
   # end
}

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/labels
$sudo rm -rf $PCP_SYSCONF_DIR/labels/*

echo "---"
pcp2json -a $A -H -I -z "" | _archive_filter
echo "---"
pcp2json -a $A -H -I -z -E "" | _archive_filter | _value_filter
echo "---"
pcp2json -a $A -H -I -Z UTC+0 -x "" | _archive_filter
echo "---"
pcp2json -a $A -H -I -z -X -b GB -P 2 -F $tmp.outfile ""
echo "---"
pcp2openmetrics -s1 -z hinv.ncpu | _filter_pcp2openmetrics
echo "---"
pcp2openmetrics -s2 -x hinv.ncpu | _filter_pcp2openmetrics
echo "---"
pcp2opentelemetry -s1 -H -z hinv.ncpu | _filter_pcp2opentelemetry
echo "---"

cat $tmp.outfile | _archive_filter
which json_verify > /dev/null 2>&1
if [ $? -eq 0 ]; then
    json_verify < $tmp.outfile > /dev/null 2>&1
    [ $? -ne 0 ] && echo "JSON is not valid!"
fi
rm -f $tmp.outfile
echo "---"

# success, all done
status=0
exit
